//
//  YBEarnDetailInviteEarnViewController.m
//  Nectar
//
//  Created by famer on 2023/11/11.
//  Copyright © 2023 asance. All rights reserved.
//

#import "YBEarnDetailInviteEarnViewController+LayoutConstraints.h"
#import "HomeCoinManager.h"
#import "RevDataGetInviteIncomeList.h"
#import "YBEarnDetailInviteEarnTableViewCell.h"

@interface YBEarnDetailInviteEarnViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(strong, nonatomic) NSMutableArray *dataSource;

@end

@implementation YBEarnDetailInviteEarnViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = kWhiteColor;

    [self.view addSubview:self.navigationBar];
    [self.view addSubview:self.contentView];
    [self.contentView addSubview:self.myTableView];

    [self setLayoutConstraints];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    [self.navigationBar resetBarItemState];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self getInviteIncomeList];
}

#pragma mark - Private Method
- (void)getInviteIncomeList{
    [SVProgressHUD show];
    self.view.userInteractionEnabled = NO;
    NSMutableDictionary *params = [NSDictionary baseReqParams];
    [params setObject:[HomeCoinManager shareInstance].globalCoinType forKey:@"coinType"];
    NSString *urlString = [NSString stringWithFormat:@"%@",PathGetInviteIncomeList];
    
    [[YBHttpRequestManager shareInstance] Post:urlString params:params success:^(NSObject *resultObject) {
        NSLog(@"getTransferInEarnList: %@",resultObject);
        
        self.view.userInteractionEnabled = YES;
        [SVProgressHUD dismiss];
        
        //判断网络是否返回错误信息
        YBResponseModel *obj = [YBResponseModel initWithResponseObject:(NSDictionary *)resultObject];
        if([obj success]){
            NSArray *list = [RevDataGetInviteIncomeList mj_objectArrayWithKeyValuesArray:obj.data];
            
            for(int i=0;i<list.count;i++){
                RevDataGetInviteIncomeList *item = [list objectAtIndex:i];
                [item calculaeCellHeight];
            }
            [self.dataSource removeAllObjects];
            [self.dataSource addObjectsFromArray:list];
//            //测试
            if(list.count==0){
                RevDataGetInviteIncomeList *item = [[RevDataGetInviteIncomeList alloc] init];
                [item setTime:@"xxx-xxx-xxx"];
                [item setRate:@"0.2"];
                [item setIncome:@"8482"];
                [item setEmail:@"8482"];
                [item calculaeCellHeight];

                [self.dataSource addObject:item];
            }
            [self.myTableView reloadData];
        }
        
    } failure:^(NSError *error) {
        self.view.userInteractionEnabled = YES;
        YBErrorModel *yerror = [YBErrorModel ErrorObjForNSError:error];
    }];
}


#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    RevDataGetInviteIncomeList *data = self.dataSource[indexPath.row];
    return data.cellHeight;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifier = @"YBEarnDetailInviteEarnTableViewCell";
    YBEarnDetailInviteEarnTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    RevDataGetInviteIncomeList *data = self.dataSource[indexPath.row];
    [cell setData:data];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    RevDataGetCoins *data = [self.dataSource objectAtIndex:indexPath.row];
}


#pragma mark - Getter Setter
YBViewGetter(contentView, _contentView, kThemeBackground);

- (YBNavigationBar *)navigationBar{
    if(!_navigationBar){
        _navigationBar = [[YBNavigationBar alloc] initWithFrame:CGRectZero];
        _navigationBar.backgroundColor = kWhiteColor;
        [_navigationBar setTitle:@"Invite Detail" titleColor:kThemeBlackColor];
        [_navigationBar setLeftBarImage:@"icon_nav_back" forState:UIControlStateNormal];
        __weak __typeof(&*self)weakSelf = self;
        _navigationBar.onLeftBarButtonBlock = ^(){
            __strong __typeof(&*weakSelf)strongSelf = weakSelf;
            if(!strongSelf) return;

            [strongSelf.navigationController popViewControllerAnimated:YES];
        };
    }
    return _navigationBar;
}

- (UITableView *)myTableView{
    if(!_myTableView){
        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _myTableView.backgroundColor = kClearColor;
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        
        [_myTableView registerClass:[YBEarnDetailInviteEarnTableViewCell class] forCellReuseIdentifier:@"YBEarnDetailInviteEarnTableViewCell"];
    }
    return _myTableView;
}


- (NSMutableArray *)dataSource{
    if(!_dataSource){
        _dataSource = [[NSMutableArray alloc] init];
    }
    return _dataSource;
}


@end
